package com.codef.sharp.common.swagger.config;

import com.codef.sharp.common.swagger.prop.SwaggerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author zhanrf
 * @date 2022/1/20 23:56
 */
@Configuration
@EnableSwagger2WebMvc
@ConditionalOnProperty(name = "swagger.enabled", havingValue = "true")
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig {
	@Bean
	public Docket api(SwaggerProperties swaggerProperties) {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo(swaggerProperties))
				.select()
				//这里指定Controller扫描包路径
				.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
				.paths(PathSelectors.any())
				.build();
	}

	/**
	 * 配置swagger文档显示的相关内容标识(信息会显示到swagger页面)
	 */
	private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
		return new ApiInfoBuilder()
				.title(swaggerProperties.getTitle())
				.description(swaggerProperties.getDescription())
				.license(swaggerProperties.getLicense())
				.licenseUrl(swaggerProperties.getLicenseUrl())
				.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
				.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
				.version(swaggerProperties.getVersion())
				.build();
	}
}
